import tkinter as tk

# 八位二进制减法计算原理
def bit_add(a, b):
    a2 = bin(a)[2:]
    b2 = bin(b)[2:]
    # b是负数
    if b < 0:
        bs = bin(b)[3:]
        b2 = '-' + bs
    # 补充符号位
    fa = f_h(a2)
    fb = f_h(b2)
    # 补码
    bm_a = base2_bm(fa[0], fa[1])
    bm_b = base2_bm(fb[0], fb[1])
    print(a, a2, bm_a)
    print(b, b2, bm_b)
    # 显示结果
    last = base2_add(bm_a, bm_b)
    c_entry.delete(0, tk.END)
    c_entry.insert(0, last)
    num = 0
    if last[0] == '1':
        # 负数
        num = -int(last[1:], 2)
    else:
        # 正数
        num = int(last, 2)
    d_entry.delete(0, tk.END)
    d_entry.insert(0, num)

# 判断符号
def f_h(n):
    a = []
    s = n[0]
    if s == '-':
        a.append('-')
        a.append(n[1:])
    else:
        a.append('+')
        a.append(n)
    return a

# 补零：八位二进制
def b_0(n):
    l = len(n)
    m = l - 7  # 只截取8位，多余的溢出
    if m > 0:
        return n[m:]
    else:
        # 补足至7位
        return n.zfill(7)

# 原码：添加符号位
def y_m(m, n):
    s = ""
    if m == '-':
        s = '1' + n
    else:
        s = '0' + n
    return s

# 反码：除去符号位，按位取反
def f_m(n):
    s = n
    # 截取符号位
    s1 = n[0]
    if s1 == "1":
        s2 = n[1:]
        # 按位置取反
        s2 = s2.replace('0', 'x')
        s2 = s2.replace('1', '0')
        s2 = s2.replace('x', '1')
        s = s1 + s2
    return s

# 补码：+1
def b_m(n):
    s = n
    s1 = n[0]
    if s1 == "1":
        ss = ""
        for i in range(7, -1, -1):
            if s[i] == "0":
                ss = s[:i] + "1" + ss
                break  # 跳出循环
            else:
                ss = "0" + ss
        s = ss
    return s

# 二进制补码
def base2_bm(m, n):
    n1 = b_0(n)
    n1 = y_m(m, n1)
    n1 = f_m(n1)
    n1 = b_m(n1)
    return n1

# 八位二进制加法模拟电路
def base2_add(a, b):
    s = ""
    add = "0"
    for i in range(7, -1, -1):
        if a[i] == "0" and b[i] == "0" and add == "0":  # 0+0+0=0
            s = "0" + s
        elif a[i] == "0" and b[i] == "1" and add == "0":  # 0+1+0=1
            s = "1" + s
        elif a[i] == "1" and b[i] == "0" and add == "0":  # 1+0+0=1
            s = "1" + s
        elif a[i] == "1" and b[i] == "1" and add == "0":  # 1+1+0=10 进位
            s = "0" + s
            add = "1"
        elif a[i] == "0" and b[i] == "0" and add == "1":  # 0+0+1=1
            s = "1" + s
            add = "0"
        elif a[i] == "0" and b[i] == "1" and add == "1":  # 0+1+1=10 进位
            s = "0" + s
            add = "1"
        elif a[i] == "1" and b[i] == "0" and add == "1":  # 1+0+1=10 进位
            s = "0" + s
            add = "1"
        elif a[i] == "1" and b[i] == "1" and add == "1":  # 1+1+1=11 进位
            s = "1" + s
            add = "1"
        else:
            # 异常处理
            pass
    # 截取符号位
    s1 = s[0]
    ss = ""
    if s1 == "1":
        # 减去1
        for i in range(7, -1, -1):
            if s[i] == "1":
                ss = s[:i] + "0" + ss
                break  # 跳出循环
            else:
                ss = "1" + ss
        # 反码
        s = f_m(ss)
    return s

def sum():
    a = int(a_entry.get())
    b = int(b_entry.get())
    print('输入：',a, b)
    bit_add(a, b)

# 创建主窗口
root = tk.Tk()
root.title("二进制加减运算原理")

# 创建输入框和按钮
a_entry = tk.Entry(root, width=5)
a_entry.insert(0, "3")
a_entry.pack(side=tk.LEFT)

plus_label = tk.Label(root, text="+")
plus_label.pack(side=tk.LEFT)

b_entry = tk.Entry(root, width=5)
b_entry.insert(0, "-5")
b_entry.pack(side=tk.LEFT)

equal_label = tk.Label(root, text="=")
equal_label.pack(side=tk.LEFT)

c_entry = tk.Entry(root, width=10)
c_entry.pack(side=tk.LEFT)

both_label = tk.Label(root, text="即")
both_label.pack(side=tk.LEFT)

d_entry = tk.Entry(root, width=5)
d_entry.pack(side=tk.LEFT)

calc_button = tk.Button(root, text="运算", command=sum)
calc_button.pack(side=tk.LEFT)

# 运行主循环
root.mainloop()